package com.liruirui.controller;

import com.github.pagehelper.PageInfo;
import com.liruirui.entity.PeiJian;
import com.liruirui.entity.QueryVo;
import com.liruirui.entity.WeiXiu;
import com.liruirui.service.WeiXiuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class WeiXiuController {
    @Autowired
    WeiXiuService service;

    @RequestMapping("/list")
    public String list(QueryVo vo, @RequestParam(defaultValue = "1") Integer pageNum, Model model){
        PageInfo<WeiXiu> info = service.list(vo,pageNum);
        model.addAttribute("info",info);
        model.addAttribute("vo",vo);
        return "weiXiu_list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(WeiXiu weiXiu, Model model){
        model.addAttribute("weiXiu",weiXiu);
        return "weiXiu_add";
    }

    @ResponseBody
    @RequestMapping("/peiJianAll")
    public List<PeiJian> peiJianAll(){
        return service.peiJianAll();
    }

    @ResponseBody
    @RequestMapping("/upFile")
    public String upFile(MultipartFile file) throws IOException {
        String path = "D://img";
        String fileName = UUID.randomUUID().toString()+file.getOriginalFilename();
        File file1 = new File(path, fileName);
        file.transferTo(file1);
        return "/img/"+fileName;
    }

    @RequestMapping("/add")
    public String add(WeiXiu weiXiu){
        service.add(weiXiu);
        return "redirect:list";
    }

    @RequestMapping("/toUpate")
    public String toUpate(String id,Model model){
        WeiXiu weiXiu = service.toUpdate(id);
        model.addAttribute("weiXiu",weiXiu);
        return "weiXiu_update";
    }

    @RequestMapping("/update")
    public String update(WeiXiu weiXiu){
        service.update(weiXiu);
        return "redirect:list";
    }

    @ResponseBody
    @RequestMapping("/del")
    public int del(String id){
        return service.del(id);
    }
}
